package com.neudu.ssrs_yyzxservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.ssrs_yyzxservice.common.JSONReturn;
import com.neudu.ssrs_yyzxservice.pojo.Customer;
import com.neudu.ssrs_yyzxservice.pojo.Nurserecord;
import com.neudu.ssrs_yyzxservice.service.NurserecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class NurserecordController {
    @Autowired
    private NurserecordService nurserecordService;

    @Autowired
    private JSONReturn jsonReturn;

    @PostMapping("/get_all_nursing")
    public String getAllNursing(@RequestBody PageInfo pageInfo) {
        try {
            PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
            //可以根据数据库设置一个state字段值，匹配在住的客户查询
            List<Nurserecord> list = nurserecordService.list();
            PageInfo<Nurserecord> pageInfoReturn = new PageInfo<>(list);
            return jsonReturn.returnSuccess(pageInfoReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    /**
     * 根据客户ID查询护理记录
     * @param nurserecord 护理记录对象
     * @return 查询到的护理记录列表
     */
    @PostMapping("/get_nursing_by_customer_id")
    public String getNursingByCustomerId(@RequestBody Nurserecord nurserecord) {
        try {
            LambdaQueryWrapper<Nurserecord> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(!ObjectUtils.isEmpty(nurserecord.getCustomerId()), Nurserecord::getCustomerId, nurserecord.getCustomerId());
            List<Nurserecord> list = nurserecordService.list(queryWrapper);
            return jsonReturn.returnSuccess(list);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    /**
     * 新增护理记录
     * @param nurserecord 护理记录对象
     * @return 操作结果
     */
    @PostMapping("/add_one_nursing")
    public String addOneNursing(@RequestBody Nurserecord nurserecord) {
        try {
            nurserecordService.save(nurserecord);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    /**
     * 根据ID获取护理记录详情
     * @param id 护理记录ID
     * @return 查询到的护理记录对象
     */
    @GetMapping("/get_nursing_by_id")
    public String getNursingById(@RequestParam("id") Integer id) {
        try {
            Nurserecord nurserecord = nurserecordService.getById(id);
            return jsonReturn.returnSuccess(nurserecord);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    /**
     * 修改护理记录
     * @param nurserecord 护理记录对象
     * @return 操作结果
     */
    @RequestMapping("/modify_nursing_by_id")
    public String modifyNursingById(@RequestBody Nurserecord nurserecord) {
        try {
            nurserecordService.updateById(nurserecord);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    /**
     * 删除护理记录
     * @param id 护理记录ID
     * @return 操作结果
     */
    @RequestMapping("/remove_nursing_by_id")
    public String removeNursingById(@RequestParam("id") Integer id) {
        try {
            nurserecordService.removeById(id);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
}